package com.treasure.service;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Arrays;

import org.apache.tomcat.jdbc.pool.DataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.scheduling.annotation.EnableScheduling;
//import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.TransactionManagementConfigurer;

@SpringBootApplication
@EnableAutoConfiguration
@EnableScheduling
@EnableTransactionManagement
@EnableConfigurationProperties
@Configuration
@ServletComponentScan
@ComponentScan(basePackages = "com.treasure.service")
public class MobileApplication extends  SpringBootServletInitializer implements TransactionManagementConfigurer, CommandLineRunner {

    private Logger logger = LoggerFactory.getLogger(MobileApplication.class);
    @Autowired
    private DataSource dataSource;
    @Autowired
    private Environment environment;

    public static void main(String[] args) {
        SpringApplication.run(MobileApplication.class, args);
    }

//    @Bean
//    public AuthHandlerInterceptor authHandlerInterceptor(){
//        return new AuthHandlerInterceptor();
//    }

//    @Bean
//    public EmbeddedServletContainerCustomizer containerCustomizer(){
//        return new EmbeddedServletContainerCustomizer() {
//            @Override
//            public void customize(ConfigurableEmbeddedServletContainer container) {
//                //单位为S
//                container.setSessionTimeout(604800);
//
//            }
//        };
//    }
    @Override
    public PlatformTransactionManager annotationDrivenTransactionManager() {
        logger.info("配置事务管理###########################");
        return new DataSourceTransactionManager(dataSource);
    }

    @Override
    public void run(String... strings) throws Exception {
        try {
            logger.info("\n----------------------------------------------------------\n\t" +
                            "Application '{}' is running! Access URLs:\n\t" +
                            "Local: \t\thttp://127.0.0.1:{}\n\t" +
                            "External: \thttp://{}:{}\n\t" +
                            "Profile(s): \t{}\n----------------------------------------------------------",

                    environment.getProperty("spring.application.name"),
                    environment.getProperty("server.port"),
                    InetAddress.getLocalHost().getHostAddress(),
                    environment.getProperty("server.port"),
                    Arrays.toString(environment.getActiveProfiles()));
        }catch ( UnknownHostException e){
            e.printStackTrace();
        }
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(this.getClass());
    }
}
